package com.groupbuying.cheetah.order.orderCounterFlow.controller;

import com.alibaba.druid.sql.visitor.functions.Substring;
import com.groupbuying.cheetah.common.FilterContextHandler;
import com.groupbuying.cheetah.common.PageUtils;
import com.groupbuying.cheetah.common.Query;
import com.groupbuying.cheetah.order.orderCounterFlow.dao.OrderCounterFlowDao;
import com.groupbuying.cheetah.order.orderCounterFlow.domain.OrderCounterFlowDo;
import com.groupbuying.cheetah.order.orderCounterFlow.service.OrderCounterFlowService;
import org.apache.ibatis.mapping.ResultMap;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@RestController
@RequestMapping("/orderForCustomer")
public class OrderCounterFlowController {

    @Autowired
    private OrderCounterFlowDao orderCounterFlowDao;

    /**
     * 我的订单列表
     */
    @GetMapping("/orderListForCustomer")
    public PageUtils orderCounterFlowList(@RequestParam Map<String, Object> params){
        String customerId = FilterContextHandler.getUserID();
        params.put("customerId",customerId);
        Query query = new Query(params);
        //优惠买单、够样优店列表
        List<OrderCounterFlowDo> orderCounterFlowList = orderCounterFlowDao.orderCounterFlowList(query);
        for (OrderCounterFlowDo orderCounterFlowDo : orderCounterFlowList) {
            //根据支付类型区分支付金额
            if ("微信支付".equals(orderCounterFlowDo.getPayType())||"支付宝支付".equals(orderCounterFlowDo.getPayType())) {
                orderCounterFlowDo.setPayAmount(orderCounterFlowDo.getPayMoney());
            } else if ("积分支付".equals(orderCounterFlowDo.getPayType())) {
                orderCounterFlowDo.setPayAmount(orderCounterFlowDo.getPayCur()+"积分");
            } else if ("余额支付".equals(orderCounterFlowDo.getPayType())) {
                orderCounterFlowDo.setPayAmount(orderCounterFlowDo.getPayBalance());
            }
            //根据订单编号5-7位区分订单类型
            String subOder = orderCounterFlowDo.getOrderId().substring(4,7);
            if ("000".equals(subOder)) {
                orderCounterFlowDo.setOrderType("够样优店");
            } else if ("001".equals(subOder)) {
                orderCounterFlowDo.setOrderType("优惠买单");
            } else if ("002".equals(subOder)) {
                orderCounterFlowDo.setOrderType("积分换购商品");
                //积分换购商品名店名称统一为“积分自营换购”
                orderCounterFlowDo.setCounterName("积分自营换购");
            }
        }
        int total = orderCounterFlowDao.count(query);
        PageUtils pageUtils = new PageUtils(orderCounterFlowList, total);
        return pageUtils;
    }
}
